package org.platform.processor.monitor.data;

import com.mc.mic.core.tool.helper.SpringContextHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.platform.dao.mapper.FdKbEventMapper;
import org.platform.mo.bussmo.diagnose.event.StandardEventBO;
import org.platform.mo.db.FdKbEvent;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 这里为了避免启动加载的先后循序,待程序启动完后再去注入数据
 *
 * @author FengJie
 * @date 2018/5/19
 */
@Component
@Slf4j
public class InitDataSource implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments var1) {
        try {
            FdKbEventMapper mapper = SpringContextHelper.getBean(FdKbEventMapper.class);
            List<FdKbEvent> fdKbEventList = mapper.selectAll();
            Map<String, List<StandardEventBO>> standardEventBOMap = CacheLibraryPO.getCacheLibraryPO().getStandardReferenceSourceMap();
            for (FdKbEvent event : fdKbEventList) {
                StandardEventBO bo = new StandardEventBO();
                BeanUtils.copyProperties(event, bo);
                String key = StringUtils.join(event.getDeviceid(), "_", event.getAttrid());
                if (standardEventBOMap.containsKey(key)) {
                    standardEventBOMap.get(key).add(bo);
                } else {
                    List<StandardEventBO> boList = new ArrayList<>();
                    boList.add(bo);
                    standardEventBOMap.put(key, boList);
                }
            }
        } catch (Exception e) {
            log.error("注入标准数据源失败", e);
        }
    }

}
